package com.lecture.java8.gui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class update extends JFrame {

    private JPanel contentPane;
    private JTextField stunoText;
    private JTextField nameText;
    private JTextField gradeText,gradeText2,gradeText3;

    private Service service=new Service();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    update frame = new update(1);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public update(final int id) {
        setTitle("Update Schedule");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 50, 443, 350);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("id:");
        lblNewLabel.setBounds(112, 50, 43, 15);
        contentPane.add(lblNewLabel);

        stunoText = new JTextField();
        stunoText.setBounds(151, 47, 160, 21);
        contentPane.add(stunoText);
        stunoText.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("date:");
        lblNewLabel_1.setBounds(112, 93, 43, 15);
        contentPane.add(lblNewLabel_1);

        nameText = new JTextField();
        nameText.setBounds(151, 90, 160, 21);
        contentPane.add(nameText);
        nameText.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("name:");
        lblNewLabel_2.setBounds(111, 134, 43, 15);
        contentPane.add(lblNewLabel_2);

        gradeText = new JTextField();
        gradeText.setBounds(151, 131, 160, 21);
        contentPane.add(gradeText);
        gradeText.setColumns(10);

        JLabel lblNewLabel_3 = new JLabel("starting:");
        lblNewLabel_3.setBounds(111, 174, 43, 15);
        contentPane.add(lblNewLabel_3);

        gradeText2 = new JTextField();
        gradeText2.setBounds(151, 171, 160, 21);
        contentPane.add(gradeText2);
        gradeText2.setColumns(10);

        JLabel lblNewLabel_4 = new JLabel("lasting:");
        lblNewLabel_4.setBounds(111, 214, 43, 15);
        contentPane.add(lblNewLabel_4);

        gradeText3 = new JTextField();
        gradeText3.setBounds(151, 211, 160, 21);
        contentPane.add(gradeText3);
        gradeText3.setColumns(10);

        //保存
        JButton saveBtn = new JButton("save");
        saveBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                int id = Integer.valueOf(stunoText.getText());
                int date=Integer.valueOf(nameText.getText());
                String name = gradeText.getText();
                String starting = gradeText2.getText();
                int lasting = Integer.valueOf(gradeText3.getText());
                if(id == 0 || "".equals(id)){
                    JOptionPane.showMessageDialog(contentPane, "Please enter the id", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if(date == 0 || "".equals(date)){
                    JOptionPane.showMessageDialog(contentPane, "Please enter the date", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if(name == null || "".equals(name)){
                    JOptionPane.showMessageDialog(contentPane, "Please enter the name of task", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if(starting == null || "".equals(starting)){
                    JOptionPane.showMessageDialog(contentPane, "Please enter the starting time", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                if(starting == null || "".equals(starting)){
                    JOptionPane.showMessageDialog(contentPane, "Please enter the lasting time", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                boolean flag = service.setTime("UPDATE timetable SET `date` = "+date+", `name` = '"+name+"', `starting` = '"+starting+"', `lasting` = "+lasting+" WHERE `id` = "+id+";");
                if(flag){
                    UserListView.load(0);  // 刷新列表
                    dispose();
                    JOptionPane.showMessageDialog(contentPane, "Modified successfully, refresh to view!");
                }else{
                    JOptionPane.showMessageDialog(contentPane, "Fail to update", "System", JOptionPane.WARNING_MESSAGE);
                }
                return;


            }
        });
        saveBtn.setBounds(151, 180, 74, 23);
        contentPane.add(saveBtn);

        //取消
        JButton cancleBtn = new JButton("cancel");
        cancleBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        cancleBtn.setBounds(237, 180, 74, 23);
        contentPane.add(cancleBtn);

        //数据回显
        User user = service.showOneTime(id);
        stunoText.setText(user.getId()+"");
        nameText.setText(user.getDate()+"");
        gradeText.setText(user.getName());
        gradeText2.setText(user.getStarting());
        gradeText3.setText(user.getLasting()+"");
    }

}